package com.ldu.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.ldu.dao.dataobject.CatelogDO;
import com.ldu.dao.intf.CatelogManager;
import com.ldu.dao.query.CatelogQuery;
import com.ldu.dao.query.support.PageResult;
import com.ldu.enums.CommonStatusEnum;
import com.ldu.pojo.Catelog;
import com.ldu.service.CatelogService;

import com.ldu.util.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

/**
 * Created by lenovo on 2017/5/9.
 */
@Service("catelogService")
public class CatelogServiceImpl implements CatelogService {

    @Resource
    private CatelogManager catelogManager;



    @Override
    public List<Catelog> getCatelogList() {
        CatelogQuery query = new CatelogQuery();
        List<CatelogDO> catelogDOS = catelogManager.selectByQuery(query);
        return catelogDOS.stream().map(this::buildCatelog).collect(Collectors.toList());

    }

    @Override
    public int getCatelogNum() {
        CatelogQuery query = new CatelogQuery();
        return catelogManager.countByQuery(query);
    }

    @Override
    public Catelog getCatelogById(Long id) {
        CatelogDO catelogDO = catelogManager.selectByPrimaryKey(id);
        return buildCatelog(catelogDO);
    }


    @Override
    public PageResult<Catelog> getPageCatelog(int pageNum, int pageSize) {
        CatelogQuery catelogQuery = new CatelogQuery();
        catelogQuery.setPageNo(pageNum);
        catelogQuery.setPageSize(pageSize);
        catelogQuery.setOrderByClause("gmt_create desc");
        PageResult<CatelogDO> pageResult = catelogManager.selectByQueryWithPage(catelogQuery);
        if (CollectionUtils.isEmpty(pageResult.getResult())) {
            return PageResult.createPageResult(Lists.newArrayList(), pageResult.getPageInfo());
        }
        List<Catelog> catelogs = pageResult.getResult().stream().map(this::buildCatelog).collect(Collectors.toList());
        return PageResult.createPageResult(catelogs, pageResult.getPageInfo());
    }

    @Override
    public List<Catelog> getPageCatelogByIdOrName(Long id,String  name, int pageNum, int pageSize) {
        CatelogQuery catelogQuery = new CatelogQuery();
        catelogQuery.setPageNo(pageNum);
        catelogQuery.setPageSize(pageSize);
        catelogQuery.setOrderByClause("gmt_create desc");
        CatelogQuery.Criteria criteria = catelogQuery.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
        if (null != id) {
            criteria.andIdEqualTo(id);
        }
        if (StringUtils.isNotBlank(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<CatelogDO> catelogDOS = catelogManager.selectByQuery(catelogQuery);
        if (CollectionUtils.isEmpty(catelogDOS)) {
            return Lists.newArrayList();
        }

        return catelogDOS.stream().map(this::buildCatelog).collect(Collectors.toList());
    }

    @Override
    public int deleteCatelog(Long id) {
        CatelogDO catelogDO = new CatelogDO();
        catelogDO.setId(id);
        catelogDO.setIsDeleted(CommonStatusEnum.YES.getCode());
        return catelogManager.updateByPrimaryKeySelective(catelogDO);
    }

    @Override
    public Boolean addCatelog(Catelog catelog) throws Exception {
        CatelogQuery query = new CatelogQuery();
        query.createCriteria().andNameEqualTo(catelog.getName());
        List<CatelogDO> catelogDOS = catelogManager.selectByQuery(query);
        if (CollectionUtils.isNotEmpty(catelogDOS)) {
            throw new Exception("分类已存在");
        }
        CatelogDO catelogDO = new CatelogDO();
        catelogDO.setName(catelog.getName());
        catelogDO.setStatus(CommonStatusEnum.YES.getCode());
        catelogManager.insertSelective(catelogDO);
        return true;
    }


    @Override
    public Catelog selectByPrimaryKey(Long id){
        CatelogDO catelogDO = catelogManager.selectByPrimaryKey(id);
        if (null == catelogDO) {
            return null;
        }
        return BeanUtil.createCloneBean(catelogDO, Catelog.class);
    }
    @Override
    public int updateByPrimaryKey(Catelog catelog) {
        CatelogDO catelogDO = BeanUtil.createCloneBean(catelog, CatelogDO.class);
        return  catelogManager.updateByPrimaryKeySelective(catelogDO);
    }

    private Catelog buildCatelog(CatelogDO catelogDO) {
        Catelog cloneBean = BeanUtil.createCloneBean(catelogDO, Catelog.class);
        cloneBean.setStatusDesc(CommonStatusEnum.getStatusDescByCode(catelogDO.getStatus()));
        return cloneBean;
    }
}
